import Common
import Network
import Data.List
import System.IO (hSetBuffering, hPutStrLn, hGetLine, BufferMode(..), Handle)
import System (getArgs)

main :: IO()
main = withSocketsDo $ do
  args <- getArgs
  let (port, _):_ = reads $ args !! 0 :: [(Int, String)]
  let host = args !! 1
  putStrLn $ "Recieving from " ++ host ++ " " ++ show port
  handle <- connectTo host $ PortNumber $ fromIntegral port
  hSetBuffering handle NoBuffering
  treeStr <- hGetLine handle
  let tree = read treeStr :: Tree Int
  putStrLn "Tree recieved:"
  print tree
  putStrLn "It's sum is:"
  print $ treeSum tree
